<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/4
 * Time: 20:29
 */

namespace app\shopping\model;

use app\common\model\Base;

class Commdetailstool extends Base{
    /**
     *  得到商品详细信息
     * $comm_sort 商品类别
     * $comm_id 商品id
     * $isdescribe 是否查询详细信息
     */
    function getCommDetails($comm_sort, $comm_id, $isdescribe) {
        $SQLB = ($isdescribe) ? "B.comm_img,B.comm_agency_type,B.comm_name,B.comm_title,B.comm_detail_text,B.comm_detail_img,B.comm_slider_img" : "B.comm_img,B.comm_agency_type,B.comm_name,B.comm_title,B.comm_img";
        switch ($comm_sort) {
            case 'consumption'://查询消费专区商品
                $sql = "SELECT A.*," . $SQLB . " FROM comm_consumption AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id WHERE A.comm_id='$comm_id'";
                return $this->selectCommDetailsConsumption($sql, $comm_sort, $isdescribe);
            case 'integral'://查询基分专区商品
                $sql = "SELECT A.*," . $SQLB . " FROM comm_integral AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id WHERE A.comm_id='$comm_id'";
                return $this->selectCommDetailsIntegral($sql, $comm_sort, $isdescribe);
            case 'activity'://查询活动专区商品
                $sql = "SELECT A.*," . $SQLB . " FROM comm_activity AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id WHERE A.comm_id='$comm_id'";
                return $this->selectCommDetailsActivity($sql, $comm_sort, $isdescribe);
            case 'merchant'://查询商户专区商品
                $uid = $_GET["uid"];
                if (empty($uid)) {
                    die(echoJson(0, "参数错误-24!"));
                }
                $sql = "SELECT A.comm_id,convert(A.comm_discount_price*(IFNULL((SELECT B.grade_discounts FROM `user` AS A LEFT JOIN agency_shop_grade AS B ON A.agency_grade_id=B.grade_id WHERE A.uid='$uid'),0)),decimal(10,2)) AS comm_discount_price,A.comm_price,A.comm_integral,A.comm_quota,A.comm_residue,A.comm_visibility,"
                    . $SQLB . "  FROM comm_merchant AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id WHERE A.comm_id='$comm_id'";
                return $this->selectCommDetailsMerchant($sql, $comm_sort, $isdescribe);
            default:
                $sql = "SELECT * FROM comm WHERE comm_id='$comm_id'";
                return $this->selectCommDetails($sql, "all", $isdescribe);
        }
    }

    /**
     *  查询消费商品
     */
    function selectCommDetailsConsumption($sql, $comm_sort, $isdescribe) {
        return selectCommDetails($sql, $comm_sort, $isdescribe);
    }

    /**
     *  查询活动商品
     */
    function selectCommDetailsActivity($sql, $comm_sort, $isdescribe) {
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            $commInfo = $this->getCommDetailsLtemArr($row, $comm_sort, $isdescribe);
            $activityInfo = $this->getActivityInfo($row);
            $commInfo = (is_array($activityInfo)) ? array_merge($activityInfo, $commInfo) : $commInfo;
            return $commInfo;
        } else {
            return false;
        }
    }

    /**
     *  得到活动信息
     */
    function getActivityInfo($row) {
        if (!isset($row["activity_type"])) {
            return false;
        }
        $commInfo = array();
        $commInfo['activity_type'] = !empty($row["activity_type"]) ? (int) $row["activity_type"] : 0;
        switch ($row["activity_type"]) {
            case 1 :
                if (!empty($row["start_time"]) && !empty($row["end_time"])) {
                    $commInfo['start_time'] = strtotime($row["start_time"]) * 1000;
                    $commInfo['end_time'] = strtotime($row["end_time"]) * 1000;
                } else {
                    $commInfo['activity_type'] = 0;
                }
                break;
            case 2 :
                if (!empty($row["activity_buy_numb"]) && !empty($row["activity_present_numb"])) {
                    $commInfo['activity_buy_numb'] = (int) $row['activity_buy_numb'];
                    $commInfo['activity_present_numb'] = (int) $row['activity_present_numb'];
                    $commInfo['activity_present_type'] = empty($row["activity_present_type"]) ? 0 : 1;
                } else {
                    $commInfo['activity_type'] = 0;
                }
                break;
            case 3 :
                if (!empty($row["activity_buy_numb"]) && !empty($row["activity_present_numb"]) && !empty($row["activity_buy_numb"]) && !empty($row["activity_present_numb"]) && !empty($row["start_time"]) && !empty($row["end_time"])) {
                    $commInfo['start_time'] = strtotime($row["start_time"]) * 1000; //php时间戳只精确到秒
                    $commInfo['end_time'] = strtotime($row["end_time"]) * 1000; //php时间戳只精确到秒
                    $commInfo['activity_buy_numb'] = (int) $row['activity_buy_numb'];
                    $commInfo['activity_present_numb'] = (int) $row['activity_present_numb'];
                    $commInfo['activity_present_type'] = empty($row["activity_present_type"]) ? 0 : 1;
                } else {
                    $commInfo['activity_type'] = 0;
                }
                break;
            default:
                $commInfo['activity_type'] = 0;
                break;
        }
        return $commInfo;
    }

    /**
     *  查询基分商品
     */
    function selectCommDetailsIntegral($sql, $comm_sort, $isdescribe) {
        return $this->selectCommDetails($sql, $comm_sort, $isdescribe);
    }

    /**
     *  查询商户商品
     */
    function selectCommDetailsMerchant($sql, $comm_sort, $isdescribe) {
        return $this->selectCommDetails($sql, $comm_sort, $isdescribe);
    }

    /**
     *   查询商品详细信息
     */
    function selectCommDetails($sql, $comm_sort, $isdescribe) {
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            return $this->getCommDetailsLtemArr($row, $comm_sort, $isdescribe);
        } else {
            return false;
        }
    }

    /**
     * 得到商品详细信息(返回单独的某一个商品的信息)
     * $row 数据库的某一条记录
     * $comm_sort 商品类型
     * $isdescribe 是否得到说明（文字加图片）
     */
    function getCommDetailsLtemArr($row, $comm_sort, $isdescribe) {
        $comm_id = $row['comm_id'];
        $comm_price = !empty($row['comm_price']) ? $row['comm_price'] : 0;
        $comm_integral = !empty($row['comm_integral']) ? (int) $row['comm_integral'] : 0;
        $comm_discount_price = !empty($row['comm_discount_price']) ? $row['comm_discount_price'] : $comm_price;
        $comm_residue = !empty($row['comm_residue']) ? (int) $row['comm_residue'] : 0; //剩余
        $comm_visibility = !empty($row['comm_visibility']) ? true : false; //是否显示
        $comm_quota = !empty($row['comm_quota']) ? (int) $row['comm_quota'] : $comm_residue; //如果限购有值且不为0，那就说明限购了，否则说明为不限购此时限购就等于剩余量
        $uid = $_GET["uid"];
        $jsonArr = array("comm_id" => $comm_id, "comm_sort" => $comm_sort, "comm_name" => $row['comm_name'], "comm_img" => $row['comm_img'],
            "comm_title" => $row['comm_title'], "comm_agency_type" => (int) $row['comm_agency_type'], "comm_price" => $comm_price, "comm_integral" => $comm_integral,
            "comm_discount_price" => $comm_discount_price, "comm_residue" => $comm_residue, "comm_visibility" => $comm_visibility,
            'comm_quota' => $comm_quota);
        if ($isdescribe) {
            $commInfo = getQuantity($uid, $comm_id, $comm_sort);
            $jsonArr['comm_quantity'] = $commInfo['comm_quantity'];
            $jsonArr["isCollect"] = isCollect($uid, $comm_id); //是否收藏
            $jsonArr["shoppingCartCommNumb"] = getShoppingCartCommNumb($uid);
            $jsonArr["comm_detail_img"] = strToJsonArr($row['comm_detail_img']);
            $jsonArr["comm_detail_text"] = strToJsonArr($row['comm_detail_text']);
            $jsonArr["comm_slider_img"] = strToJsonArr($row['comm_slider_img']);
            $jsonArr['comm_attribute'] = getAttribute($comm_id, $commInfo['activeCroperty']);
        }
        return $jsonArr;
    }

    /**
     *  得到用于加入购物车的数量，和选中的信息
     * @param str $uid 账号
     */
    function getQuantity($uid, $comm_id, $comm_sort) {
        if (empty($uid)) {
            return array('comm_quantity' => 1, 'activeCroperty' => false, 'selected_attribute' => null);
        }
        $sql = "select comm_quantity,selected_attribute from sopp_shoppingcart WHERE uid='$uid' AND comm_id='$comm_id' AND comm_sort='$comm_sort'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            $comm_quantity = (!empty($row['comm_quantity'])) ? $row['comm_quantity'] : 1;
            $activeCroperty = (!empty($row['selected_attribute'])) ? strToJsonArr($row['selected_attribute']) : false;
            $selected_attribute = getAttributeStr($activeCroperty);
            return array('comm_quantity' => $comm_quantity, 'activeCroperty' => $activeCroperty, 'selected_attribute' => $selected_attribute);
        } else {
            return array('comm_quantity' => 1, 'activeCroperty' => false, 'selected_attribute' => null);
        }
    }

    /**
     *  得到用户选择商品属性的字符串
     *  $activeCroperty 选中属性json
     */
    function getAttributeStr($activeCroperty) {
        if ($activeCroperty) {
            $selected_attribute = "已选择：";
            foreach ($activeCroperty as $key => $value) {
                $selected_attribute .= $key . ": " . $value . "；   ";
            }
        } else {
            $selected_attribute = "已选择：随机发货";
        }
        return $selected_attribute;
    }

    /**
     *  得到用户选择商品属性
     * @param str $comm_id 商品id
     * @param $selected_attribute 已经选择的属性
     */
    function getAttribute($comm_id, $selected_attribute) {
        $sql = "SELECT attribute_name,attribute_value,default_attribute FROM comm_attribute WHERE comm_id='$comm_id'";
        $result = $this->mysql->selectSQLAll($sql);
        $attributeArr = array();
        foreach ($result as $row) {
            $attribute_value = strToJsonArr($row['attribute_value']); //得到商品某个属性的所有值
            if (isset($selected_attribute[$row['attribute_name']])) {//判断是否有用户选中，没有就设置成默认值
                $attributeArr[] = array('name' => $row['attribute_name'], "active" => $selected_attribute[$row['attribute_name']], 'value' => $attribute_value);
            } else {//设置成默认值
                if (!empty($row['default_attribute'])) {
                    $attributeArr[] = array('name' => $row['attribute_name'], "active" => $row['default_attribute'], 'value' => $attribute_value);
                } else {
                    $attributeArr[] = array('name' => $row['attribute_name'], "active" => $attribute_value[0], 'value' => $attribute_value);
                }
            }
        }
        return $attributeArr;
    }

    /**
     *  得到用户购物车商品数量
     * @param str $uid 账号
     */
    function getShoppingCartCommNumb($uid) {
        if (empty($uid)) {
            return 0;
        }
        $sql = "select count(1) from sopp_shoppingcart  WHERE uid='$uid'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row && $row[0]) {
            return $row[0];
        } else {
            return 0;
        }
    }

    /**
     *  是否加入收藏
     * @param str $uid 账号
     */
    function isCollect($uid, $comm_id) {
        if (empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM sopp_collect WHERE uid='$uid' and comm_id='$comm_id'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            return true;
        } else {
            return false;
        }
    }
}